package yagwl.server.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;

import yagwl.client.model.FilterDTO;
import yagwl.client.model.ItemDTO;
import yagwl.client.model.demo.LaureateRpcService;
import yagwl.service.demo.LaureateEntity;
import yagwl.service.demo.LaureateService;
import yagwl.service.demo.LaureateServiceImpl;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class LaureateRpcServiceImpl extends RemoteServiceServlet implements
    LaureateRpcService {
  private static final Logger log = Logger
      .getLogger(LaureateRpcServiceImpl.class.getName());
  private LaureateService svc = LaureateServiceImpl.getInstance();

  @Override
  public List<ItemDTO> getLaureates(int startIndex, int fetchSize, int sortOrder) {
    List<LaureateEntity> laureates = svc.getLaureates(startIndex, fetchSize,
        sortOrder);
    List<ItemDTO> loList = copy(laureates);
    return loList;
  }

  @Override
  public List<ItemDTO> getLaureates(int startIndex, int fetchSize,
      int sortOrder, List<FilterDTO> selectedFilters) {
    String category = null;
    int yearStart = -1;
    int yearEnd = -1;
    List<String> countryList = null;
    if (selectedFilters != null)
      for (FilterDTO filter : selectedFilters) {
        if (filter.getId().equals("category") && filter.getSelectList() != null
            && filter.getSelectList().size() > 0) {
          category = filter.getSelectList().get(0).getLabel();
          continue;
        }
        if (filter.getId().equals("year")) {
          yearStart = filter.getMinValue();
          yearEnd = filter.getMaxValue();
          continue;
        }
        if (filter.getId().equals("country") && filter.getSelectList() != null) {
          countryList = new ArrayList<String>();
          for (ItemDTO item : filter.getSelectList()) {
            countryList.add(item.getLabel());
          }
        }
      }
    List<LaureateEntity> laureates = svc.getLaureates(startIndex, fetchSize,
        sortOrder, category, yearStart, yearEnd, countryList);
    List<ItemDTO> loList = copy(laureates);
    return loList;
  }

  private List<ItemDTO> copy(List<LaureateEntity> laureates) {
    List<ItemDTO> loList = new ArrayList<ItemDTO>();
    for (LaureateEntity l : laureates) {
      ItemDTO lo = new ItemDTO();
      lo.put("name", l.getName());
      lo.put("nameLink", l.getNameLink());
      lo.put("photo", l.getPhoto());
      lo.put("photoLink", l.getPhotoLink());
      lo.put("country", l.getCountry());
      lo.put("countryLink", l.getCountryLink());
      lo.put("year", l.getYear());
      lo.put("category", l.getCategory());
      lo.put("rationale", l.getRationale());
      loList.add(lo);
    }
    return loList;
  }

  @Override
  public String causeDelay() {
    try {
      Random rand = new Random();
      Thread.sleep(rand.nextInt(5000));
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return null;
  }

  @Override
  public List<FilterDTO> getFilters() {
    log.info("Entering");
    List<FilterDTO> filterList = new ArrayList<FilterDTO>();

    // category
    FilterDTO filter = new FilterDTO();
    filter.setId("category");
    filter.setLabel("Category - Single Select");
    List<ItemDTO> list = new ArrayList<ItemDTO>();
    list.add(getItemDTO("Chemistry"));
    list.add(getItemDTO("Economics"));
    list.add(getItemDTO("Literature"));
    list.add(getItemDTO("Medicine"));
    list.add(getItemDTO("Peace"));
    list.add(getItemDTO("Physics"));
    filter.setSingleSelectList(list);
    filterList.add(filter);

    // year
    filter = new FilterDTO();
    filter.setId("year");
    filter.setLabel("Year");
    filter.setIntegerRange(1900, 2020, 10);
    filterList.add(filter);

    // country
    filter = new FilterDTO();
    filter.setId("country");
    filter.setLabel("Country - Multi Select");
    List<ItemDTO> list2 = new ArrayList<ItemDTO>();
    for (String c : svc.getCountries()) {
      list2.add(getItemDTO(c));
    }
    filter.setMultiSelectList(list2);
    filterList.add(filter);

    return filterList;
  }

  private ItemDTO getItemDTO(String label) {
    ItemDTO item = new ItemDTO();
    item.setLabel(label);
    return item;
  }
}
